using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Settings;
using SettingsAddIn.Properties;

namespace UI.SettingsPanel
{

    public partial class SettingsAssociationsControl : System.Windows.Controls.UserControl, ISettingPanel
    {

        #region Variables

        private bool _updatingCheckboxes = false;
        private bool _isUpdated = false;

        #endregion

        #region Constructor

        public SettingsAssociationsControl()
        {
            InitializeComponent();

            chkCheckAssociationsAtStartup.IsChecked = Host.SettingsService.CheckAssociationsAtStartup;

            btnCheckAll.Click += new RoutedEventHandler(btnCheckAll_Click);
            btnUncheckAll.Click += new RoutedEventHandler(btnUncheckAll_Click);
            btnApply.Click += new RoutedEventHandler(btnApply_Click);
        }

        #endregion

        #region Properties

        public string Title
        {
            get
            {
                return SettingsAddIn.Properties.Globalization.SettingsPart_FilesAssociations;
            }
        }

        #endregion

        #region Check / Uncheck all

        void btnUncheckAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (UIElement control in wpMusicAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = false;
            }

            foreach (UIElement control in wpVideoAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = false;
            }

            foreach (UIElement control in wpRadioAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = false;
            }
        }

        void btnCheckAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (UIElement control in wpMusicAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = true;
            }

            foreach (UIElement control in wpVideoAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = true;
            }

            foreach (UIElement control in wpRadioAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                checkBox.IsChecked = true;
            }
        }

        #endregion

        #region Button : apply

        void btnApply_Click(object sender, RoutedEventArgs e)
        {
            Apply();
        }

        #endregion

        #region Apply

        public void Apply()
        {
            if (!_isUpdated)
                return;

            Host.SettingsService.CheckAssociationsAtStartup = (chkCheckAssociationsAtStartup.IsChecked == true);

            foreach (UIElement control in wpMusicAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                if (checkBox.IsChecked == true)
                    Host.MediaService.SetMediaFileAssociation(checkBox.Tag.ToString());
            }

            foreach (UIElement control in wpVideoAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                if (checkBox.IsChecked == true)
                    Host.MediaService.SetMediaFileAssociation(checkBox.Tag.ToString());
            }

            foreach (UIElement control in wpRadioAssociations.Children)
            {
                CheckBox checkBox = control as CheckBox;
                if (checkBox == null)
                    continue;

                if (checkBox.IsChecked == true)
                    Host.MediaService.SetMediaFileAssociation(checkBox.Tag.ToString());
            }

            _isUpdated = false;
        }

        #endregion

        #region Cancel

        public void Cancel()
        {
            _updatingCheckboxes = true;

            wpMusicAssociations.Children.Clear();
            foreach (string ext in Host.MediaService.SupportedMusicExtensions.Keys)
            {
                CheckBox extCheckBox = new CheckBox();
                TextBlock text = new TextBlock();
                text.Text = ext;
                text.Margin = new Thickness(3);
                text.Width = 50;
                //text.Foreground = FindResource("VOColor1A") as Brush;
                extCheckBox.Content = text;
                extCheckBox.Tag = ext;

                wpMusicAssociations.Children.Add(extCheckBox);

                if (Host.MediaService.IsMediaFileAssociated(ext))
                    extCheckBox.IsChecked = true;

                extCheckBox.Checked += new RoutedEventHandler(checkBox_Checked);
                extCheckBox.Unchecked += new RoutedEventHandler(checkBox_Unchecked);
            }

            wpRadioAssociations.Children.Clear();
            foreach (string ext in Host.MediaService.SupportedRadioExtensions.Keys)
            {
                CheckBox extCheckBox = new CheckBox();
                TextBlock text = new TextBlock();
                text.Text = ext;
                text.Margin = new Thickness(3);
                text.Width = 50;
                //text.Foreground = FindResource("VOColor1A") as Brush;
                extCheckBox.Content = text;
                extCheckBox.Tag = ext;

                wpRadioAssociations.Children.Add(extCheckBox);

                if (Host.MediaService.IsMediaFileAssociated(ext))
                    extCheckBox.IsChecked = true;

                extCheckBox.Checked += new RoutedEventHandler(checkBox_Checked);
                extCheckBox.Unchecked += new RoutedEventHandler(checkBox_Unchecked);
            }

            wpVideoAssociations.Children.Clear();
            foreach (string ext in Host.MediaService.SupportedVideoExtensions.Keys)
            {
                CheckBox extCheckBox = new CheckBox();
                TextBlock text = new TextBlock();
                text.Text = ext;
                text.Margin = new Thickness(3);
                text.Width = 50;
                //text.Foreground = FindResource("VOColor1A") as Brush;
                extCheckBox.Content = text;
                extCheckBox.Tag = ext;

                wpVideoAssociations.Children.Add(extCheckBox);

                if (Host.MediaService.IsMediaFileAssociated(ext))
                    extCheckBox.IsChecked = true;

                extCheckBox.Checked += new RoutedEventHandler(checkBox_Checked);
                extCheckBox.Unchecked += new RoutedEventHandler(checkBox_Unchecked);
            }

            _isUpdated = false;
            _updatingCheckboxes = false;
        }

        #endregion

        #region Reset

        public void Reset()
        {
            // Reset
            Cancel();
        }

        #endregion

        #region Check / Uncheck

        void checkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_updatingCheckboxes)
                return;
            _isUpdated = true;
        }

        void checkBox_Checked(object sender, RoutedEventArgs e)
        {
            if (_updatingCheckboxes)
                return;
            _isUpdated = true;
        }

        #endregion

    }
}